<script lang="ts" setup>
import { toRefs } from 'vue'
import type { UploadProps } from 'element-plus'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import { Result } from '../../../../typings'
import { saveUser } from '@/api/user'
import { useUserStore } from '@/stores/user'
import router from '@/router'
const { userInfo } = toRefs(useUserStore())
const handleAvatarSuccess: UploadProps['onSuccess'] = (response: Result<{ url: string }>) => {
  userInfo.value.avatar = response.result.url
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
const submit = () => {
  console.log("点击成功")
  saveUser(userInfo.value).then((res) => {
    ElMessage("修改成功")
    console.log("修改成功")
   
  })
}
const loginout = () => {
  console.log("等处")
  router.replace({ path: '/login' })
  ElMessage("登出成功")
  localStorage.setItem('isAuthenticated', 'false')
}
</script>

<template>
  <div class="profile-view">
    <div class="title">我的信息</div>
    <el-form class="form" label-position="top">
      <el-form-item label="昵称">
        <el-input v-model="userInfo.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type = "password" v-model="userInfo.password"></el-input>
      </el-form-item>
      
    </el-form>
    <el-button type="success" @click="submit"> 提交修改</el-button>
    <el-button type="success" @click="loginout"> 退出登录</el-button>
  </div>
</template>

<style lang="scss" scoped>
.profile-view {
  .title {
    font-size: 18px;
    margin-bottom: 20px;
  }

  .avatar-uploader .avatar {
    object-fit: cover;
    width: 120px;
    height: 120px;
    display: block;
  }
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
}
</style>
